package com.agentai.buiness.back.config.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.agentai.framework.common.utils.PageResult;
import com.agentai.framework.mybatis.service.impl.BaseServiceImpl;
import com.agentai.buiness.back.config.convert.SysConfigConvert;
import com.agentai.buiness.back.config.entity.SysConfigEntity;
import com.agentai.buiness.back.config.query.SysConfigQuery;
import com.agentai.buiness.back.config.vo.SysConfigVO;
import com.agentai.buiness.back.config.mapper.SysConfigMapper;
import com.agentai.buiness.back.config.service.SysConfigService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 系统参数设置表
 *
 * @since 1.0.0 2024-10-31
 */
@Service
@AllArgsConstructor
public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysConfigEntity> implements SysConfigService {

    @Override
    public PageResult<SysConfigVO> page(SysConfigQuery query) {
        IPage<SysConfigEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(SysConfigConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<SysConfigEntity> getWrapper(SysConfigQuery query){
        LambdaQueryWrapper<SysConfigEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(SysConfigVO vo) {
        SysConfigEntity entity = SysConfigConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SysConfigVO vo) {
        SysConfigEntity entity = SysConfigConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 30)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}
